package com.china_goldcard.paydialog;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jk on 16/11/1.
 */

public class PayDialog extends ViewFlipper{

    private View payOrderView;

    private View payWayView;

    private RecyclerView paywayListV;

    private RecyclerView payOrderInfoListV;

    private PayOrderAdapter payOrderAdapter;

    private PayWayAdapter payWayAdapter;

    private ImageView backImageView;

    private List<PayWayModel> payWayModels = new ArrayList<>();

    private List<PayOrderModel> payOrderModels = new ArrayList<>();

    public PayDialog(Context context) {
        this(context,null);
    }

    public PayDialog(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    /**
     * 初始化视图
     */
    private void initView(Context context){
        payOrderView = LayoutInflater.from(context).inflate(R.layout.pay_dialog_order_view,this,false);
        payOrderInfoListV = (RecyclerView) payOrderView.findViewById(R.id.pay_dialog_list_view);
        payWayView = LayoutInflater.from(context).inflate(R.layout.pay_dialog_pay_way_view,this,false);
        paywayListV = (RecyclerView) payWayView.findViewById(R.id.pay_dialog_pay_list_view);
        backImageView = (ImageView)payWayView.findViewById(R.id.pay_dialog_pay_back);
        addView(payOrderView);
        addView(payWayView);
        payWayAdapter = new PayWayAdapter(payWayModels,getContext());
        payOrderAdapter = new PayOrderAdapter(payOrderModels,getContext());
        LinearLayoutManager orderInfoLayoutManager = new LinearLayoutManager(context);
        orderInfoLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        LinearLayoutManager payInfoLayoutManager = new LinearLayoutManager(context);
        payInfoLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        payOrderInfoListV.setLayoutManager(orderInfoLayoutManager);
        payOrderInfoListV.setAdapter(payOrderAdapter);
        paywayListV.setLayoutManager(payInfoLayoutManager);
        paywayListV.setAdapter(payWayAdapter);
        payOrderAdapter.setOnItemClickListener(new PayOrderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, PayOrderModel model) {
                switch (model.getType()){
                    case PayWayInfo:
                        showNext();
                        break;
                    default:
                        break;
                }
            }
        });

        backImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showPrevious();
            }
        });

        Animation animIn = AnimationUtils.loadAnimation(context, R.anim.anim_right_in);
        setInAnimation(animIn);

        Animation animOut = AnimationUtils.loadAnimation(context, R.anim.anim_left_out);
        setOutAnimation(animOut);
    }


    /**
     * 加载订单信息
     * @param dataList
     */
    public void reloadOrderInfo(List<PayOrderModel> dataList){
        this.payOrderModels = dataList;
        payOrderAdapter.reloadData(this.payOrderModels);
    }

    /**
     * 加载支付信息
     * @param dataList
     */
    public void reloadPayWayInfo(List<PayWayModel> dataList){
        this.payWayModels = dataList;
        payWayAdapter.reloadData(this.payWayModels);
    }

}
